﻿
Public Module _float_bytes
    Public Function bytes_single(ByVal i() As Byte,
                                 ByRef o As Single,
                                 Optional ByRef offset As UInt32 = 0) As Boolean
        If array_size(i) >= offset + sizeof_single Then
            o = BitConverter.ToSingle(i, offset)
            offset += sizeof_single
            Return True
        Else
            Return False
        End If
    End Function

    Public Function entire_bytes_single(ByVal i() As Byte, ByRef o As Single) As Boolean
        Dim p As UInt32 = 0
        Return bytes_single(i, o, p) AndAlso
               p = array_size(i)
    End Function

    Public Function bytes_single(ByVal i() As Byte,
                                 Optional ByRef offset As UInt32 = 0) As Single
        Dim r As Single = 0
        k_assert(bytes_single(i, r, offset))
        Return r
    End Function

    Public Function entire_bytes_single(ByVal i() As Byte) As Single
        Dim o As Single = 0
        k_assert(entire_bytes_single(i, o))
        Return o
    End Function

    Public Function bytes_double(ByVal i() As Byte,
                                 ByRef o As Double,
                                 Optional ByRef offset As UInt32 = 0) As Boolean
        If array_size(i) >= offset + sizeof_double Then
            o = BitConverter.ToDouble(i, offset)
            offset += sizeof_double
            Return True
        Else
            Return False
        End If
    End Function

    Public Function entire_bytes_double(ByVal i() As Byte, ByRef o As Double) As Boolean
        Dim p As UInt32 = 0
        Return bytes_double(i, o, p) AndAlso
               p = array_size(i)
    End Function

    Public Function bytes_double(ByVal i() As Byte,
                                 Optional ByRef offset As UInt32 = 0) As Double
        Dim r As Double = 0
        k_assert(bytes_double(i, r, offset))
        Return r
    End Function

    Public Function entire_bytes_double(ByVal i() As Byte) As Double
        Dim o As Double = 0
        k_assert(entire_bytes_double(i, o))
        Return o
    End Function

    Public Function single_bytes(ByVal i As Single,
                                 ByVal d() As Byte,
                                 Optional ByRef offset As UInt32 = 0) As Boolean
        If array_size(d) >= offset + sizeof_single Then
            Dim t() As Byte = Nothing
            t = single_bytes(i)
            memcpy(d, offset, t)
            offset += sizeof_single
            Return True
        Else
            Return False
        End If
    End Function

    Public Function single_bytes(ByVal i As Single) As Byte()
        Return BitConverter.GetBytes(i)
    End Function

    Public Function double_bytes(ByVal i As Double,
                                 ByVal d() As Byte,
                                 Optional ByRef offset As UInt32 = 0) As Boolean
        If array_size(d) >= offset + sizeof_double Then
            Dim t() As Byte = Nothing
            t = double_bytes(i)
            memcpy(d, offset, t)
            offset += sizeof_double
            Return True
        Else
            Return False
        End If
    End Function

    Public Function double_bytes(ByVal i As Double) As Byte()
        Return BitConverter.GetBytes(i)
    End Function
End Module
